<template>
  <div class="detail-page">
    <van-nav-bar
      left-text="返回"
      fixed
      title="面经详细"
      @click-left="$router.back()"
    />
    <header class="header">
      <h1>{{ ArticleLisr.stem }}</h1>
      <p>
        {{ ArticleLisr.createdAt }} | {{ ArticleLisr.views }} 浏览量 |
        {{ ArticleLisr.likeCount }} 点赞数
      </p>
      <p>
        <img :src="ArticleLisr.avatar" alt="" />
        <span>{{ ArticleLisr.creator }}</span>
      </p>
    </header>
    <main class="body" v-html="ArticleLisr.content"></main>
    <div class="opt">
      <van-icon
        @click="LikeCollect(1)"
        :class="{ active: like_flag }"
        name="like-o"
      />
      <van-icon
        @click="LikeCollect(2)"
        :class="{ active: collect_flag }"
        name="star-o"
      />
    </div>
  </div>
</template>

<script>
import { Axiosdateai, AxiosLikeCollect } from '@/api/user'
export default {
  name: 'detail-page',
  data () {
    return {
      ArticleLisr: {},
      like_flag: false,
      collect_flag: false
    }
  },
  async created () {
    const { data: res } = await Axiosdateai({
      id: this.$route.params.id
    })
    this.ArticleLisr = res
    console.log(res)
  },

  methods: {
    // 点赞收藏
    async LikeCollect (num) {
      if (num == 1) {
        this.like_flag = !this.like_flag
        localStorage.setItem('like_flag', this.like_flag)
        await AxiosLikeCollect({
          id: this.ArticleLisr.id,
          optType: num
        })
        this.like_flag
          ? this.$toast.success('点赞成功')
          : this.$toast.success('取消点赞')
      } else {
        this.collect_flag = !this.collect_flag

        this.collect_flag
          ? this.$toast.success('收藏成功')
          : this.$toast.success('收藏取消')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;
  .header {
    h1 {
      font-size: 24px;
    }
    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;
    > .van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;
      &.active {
        background: #fec635;
        color: #fff;
      }
    }
  }
}
</style>
